package com.spzx.product.receiver;

import com.rabbitmq.client.Channel;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.product.service.IProductService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ProductReceiver {

    @Autowired
    private IProductService productService;

    //解锁库存
    @SneakyThrows
    @RabbitListener(bindings =
            @QueueBinding(
                    exchange = @Exchange(value = MqConst.EXCHANGE_PRODUCT,durable = "true"),
                    value = @Queue(value = MqConst.QUEUE_UNLOCK,durable = "ture"),
                    key = MqConst.ROUTING_UNLOCK
            )
    )
    public void unlock(String orderNo, Message message, Channel channel){
        if (StringUtils.isNotEmpty(orderNo)){
            log.info("解锁库存"+orderNo);
            productService.unlock(orderNo);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    //减库存
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_PRODUCT,durable = "true"),
            value = @Queue(value = MqConst.QUEUE_MINUS,durable = "true"),
            key = MqConst.ROUTING_MINUS
    ))
    public void  minus(String orderNo,Message message,Channel channel){
        if (StringUtils.isNotEmpty(orderNo)){
            log.info("减库存:"+orderNo);
            productService.minus(orderNo);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}